package cn.yoyo.service;

import cn.yoyo.entity.*;
import cn.yoyo.mapper.BookMapper;
import cn.yoyo.mapper.ItemMapper;
import cn.yoyo.mapper.OrderMapper;
import cn.yoyo.tools.GetTime;
import cn.yoyo.tools.GetUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    BookMapper bookMapper;

    @Override
    @Transactional
    public int add(List<Item> items) {
        SysUser user = GetUser.getUser();
        // 遍历数组获取订单的总价
        Double amount = 0.0;
        for (int i = 0; i < items.size(); i++) {
            amount = amount + (double) items.get(i).getPrice() * items.get(i).getNum();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("uid", user.getId());
        map.put("amount", amount);
        map.put("ispay", 0);
        map.put("tpay", null);
        map.put("address", "");
        map.put("phone", "");
        map.put("tcreate", GetTime.getDate(new Date()));
        map.put("shopno", "");
        // 给 selectKey 返回的自增长主键值腾位置
        map.put("ordid", "");
        orderMapper.add(map);
        System.out.println("最新增加的一条ordid是:" + map.get("ordid"));
        // 前端传过来的CartBook有多个,
        // 通过for循环进行多次sql操作 生成多个订单明细
        for (int i = 0; i < items.size(); i++) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("ordid", map.get("ordid"));
            map1.put("bookid", items.get(i).getBookid());
            map1.put("num", items.get(i).getNum());
            map1.put("price", items.get(i).getPrice());
            itemMapper.add(map1);
        }
        return (Integer) map.get("ordid");
    }

    @Override
    public List<OrderLine> findOrderByInfo(Map<String, Object> map) {
        Integer start = (Integer) map.get("start");
        Integer num = (Integer) map.get("num");
        if (start < 1 || num < 1) return null;
        // 数据库数据从0开始查
        Integer pageNow = 0;
        if (start >= 1) {
            pageNow = (start - 1) * num;
        }
        map.put("start", pageNow);
        return orderMapper.findOrderByInfo(map);
    }

    @Override
    public List<OrderLine> findOrderLines(Integer start, Integer num) {
        if (start < 1 || num < 1) return null;
        Integer pageNow = 1;
        if (start >= 1) {
            pageNow = (start - 1) * num;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("start", pageNow);
        map.put("num", num);
        return orderMapper.findOrderLines(map);
    }


    @Override
    public List<Order> findOrdLineByPage(Map<String, Object> map) {
        SysUser user = GetUser.getUser();
        Integer start = (Integer) map.get("start");
        Integer num = (Integer) map.get("num");
        if (start < 1 || num < 1) return null;
        // 数据库数据从0开始查
        Integer pageNow = 0;
        if (start >= 1) {
            pageNow = (start - 1) * num;
        }
        map.put("start", pageNow);
        map.put("uid", user.getId());
        for (String str : map.keySet()) {
            System.out.println(str + "===" + map.get(str));
        }
        return orderMapper.findOrdLineByPage(map);
    }

    @Override
    public int getTotal(Map<String, Object> map) {
        return orderMapper.getTotal(map);
    }

    @Override
    public int addAddress(String address, String phone, Integer ordid) {
        Map<String, Object> map = new HashMap<>();
        map.put("address", address);
        map.put("phone", phone);
        map.put("ordid", ordid);
        return orderMapper.addAddress(map);
    }

    @Override
    @Transactional
    public int addPay(Integer ordid) {
        Map<String, Object> map = new HashMap<>();
        map.put("ordid", ordid);
        map.put("ispay", 1);
        map.put("tpay", GetTime.getDate(new Date()));
        int x = orderMapper.addPay(map);

        // 用户付款后,订单里的书相应的 Salenum(销售量)会加上订单明细里的num
        OrderInfo orderInfo = orderMapper.findItemInfoByOrdid(ordid);// 获取订单所有的订单明细
        for (int i = 0; i < orderInfo.getItems().size(); i++) {
            Book book = bookMapper.findById(orderInfo.getItems().get(i).getBookid());//获取订单里的书本信息
            Map<String, Object> map1 = new HashMap<>();
            map1.put("id", book.getId());// 修改书本的销售数量
            map1.put("salenum", book.getSalenum() + orderInfo.getItems().get(i).getNum());
            bookMapper.updateRFS(map1);
        }
        return x;
    }

    @Override
    public int send(Map<String, Object> map) {
        return orderMapper.send(map);
    }

    @Override
    public OrderInfo findItemInfoByOrdid(Integer orderid) {
        return orderMapper.findItemInfoByOrdid(orderid);
    }

    @Override
    public List<ItemBook> findInfoByOrdid(Integer ordid) {
        return orderMapper.findInfoByOrdid(ordid);
    }

}
